package br.com.iswe.recipemaster.gwt.client;

import br.com.iswe.gwt.ui.client.BaseModuloEntryPoint;
import br.com.iswe.gwt.ui.client.ClientInfoContext;
import br.com.iswe.gwt.ui.client.SessionInfo;

import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public abstract class RmBaseModule extends BaseModuloEntryPoint {
	

	private static final String TRUE = "true";
	private static final String NEWUSER = "newuser";
	private static final String ADMINUSER = "adminuser";


	protected Widget buildLoginScreen() {
		return null;//this method should never be called
	}
	
	/* (non-Javadoc)
	 * @see br.com.iswe.gwt.ui.client.BaseModuloEntryPoint#showTelaLogin()
	 */
	@Override
	protected void showTelaLogin() {
		//redirect to the index.jsp
		redirectToUrl("/index.jsp");
	}
	
	protected abstract void showNewUserScreen();
		
	protected abstract void showAdminUserScreen();
	
	@Override
	protected void sessionInfoReceived(SessionInfo sessionInfo) {
		//configura a info do cliente no clientIndoContext para que
		//nos proximos requests na tenha a informacao para ser enviada.
		ClientInfoContext.setClientInfoContext(sessionInfo);
		
		//verify if it is a new user
		String newUserFlag = sessionInfo.getPropriedades().getString(NEWUSER);
		String adminFlag = sessionInfo.getPropriedades().getString(ADMINUSER);
		
		if(adminFlag != null && TRUE.equals(adminFlag)){
			//show the admin screen
			showNewUserScreen();
		}
		else if(newUserFlag != null && TRUE.equals(newUserFlag)){
			// show the new user screen
			showNewUserScreen();
		}
		else{	
			//mostra a ultima tela
			showLastWidget();
			//mostra os dados do usuario e botao de logou.
			showInfoUsuario();
		}
	}
	
	
	protected static native void redirectToUrl(String url)/*-{
    	$wnd.location = url;
	}-*/; 
}